import store from '@/store'
import { Toast } from 'vant'
import { EventBus } from '@/store/index'

export default class BaseCreatejs {
  constructor() {
    this.stage;
    this.stagePage;
    this.lib;

    this.item;
    this.commonAttr;
    this.store = store;
  }

  get componentMap() {
    return this.store.state.componentMap;
  }
  get controls() {
    return this.store.state.controls ? this.store.state.controls : this.store.state.wbComponents.controls;
  }
  get allPages() {
    return this.store.state.allPages ? this.store.state.allPages : this.store.state.components.controls;
  }
  get wbUserData() {
    return this.store.state.userInfo.wbUserData;
  }
  get userInfo() {
    return this.store.state.userInfo;
  }
  get interactionData() {
    return this.store.state.interactionData;
  }
  get frontData() {
    return this.store.state.frontData;
  }
  get formValues() {
    return this.store.state.formValues;
  }
  get globalIsPost() {
    return this.store.state.globalIsPost;
  }
  get pages() {
    return this.store.state.pages;
  }
  get pops() {
    return this.store.state.pops_;
  }
  get statePopId() {
    return this.store.state.popId;
  }
  get pageIndex() {
    return this.store.state.pageIndex;
  }
  get siteAttrs() {
    return this.store.state.attrs;
  }

  get timeObj() {
    return this.store.state.timeObj;
  }

  get appid() {
    return this.store.state.attrs.wb_appkey;
  }
  get uid() {
    return this.store.state.userInfo.uid;
  }
  get siteInfo() {
    return this.store.state.siteInfo;
  }

  init({ stage, item, stagePage, lib }) {
    this.stage = stage;
    this.item = item;
    if (stagePage) {
      this.stagePage = stagePage;
    } else {
      this.stagePage = new window.createjs.Container();
    }
    this.lib = lib;
    this.commonAttr = this.item.commonAttr;
    this.initView();
    this.stage.addChild(this.stagePage);
  }

  initView() {}

  showToast(msg) {
    if (_.trim(msg)) {
      Toast(msg);
    }
  }
  showPop(popId) {
    if(!this.pops[popId-1]){
      return
    }
    if (this.statePopId > 0 && this.statePopId != popId) {
      EventBus.$emit('closePop')
    }
    this.store.commit('updatePopId', { popId })
    EventBus.$emit("showPop", { popId })
  }

  destroy() {
    this.stage = null;
    this.stagePage = null;
    this.lib = null;
    this.item = null;
    this.commonAttr = null;
    this.store = null;
  }
}
